package com.chaochis.wind.service.impl;

import com.chaochis.wind.common_exceptions.not_exist_exception.AccountNotExistException;
import com.chaochis.wind.common_exceptions.not_exist_exception.UserNotExistException;
import com.chaochis.wind.enums.AccountType;
import com.chaochis.wind.enums.DataStatus;
import com.chaochis.wind.mapper.WindAccountMapper;
import com.chaochis.wind.mapper.WindUserMapper;
import com.chaochis.wind.service.WindAccountService;
import com.chaochis.wind.service.WindUserService;
import com.chaochis.wind.vo.WindAccount;
import com.chaochis.wind.vo.WindUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("windAccountService")
public class WindAccountServiceImpl implements WindAccountService {
  protected WindAccountMapper windAccountMapper;
  
  protected WindUserService windUserService;
  private WindUserMapper windUserMapper;
  
  
  /**
   * 获取所有的登录账户
   * @param windAccount 要查询的参数
   * @return 返回查询到的登录账户
   */
  @Override
  public List<WindAccount> getWindAccountList(WindAccount windAccount) {
    return this.windAccountMapper.getAccountList(windAccount);
  }
  
  @Override
  public WindAccount getWindAccountByAccountName(String accountName, AccountType accountType) {
    var windAccount = new WindAccount();
    windAccount.setAccountName(accountName);
    windAccount.setAccountType(accountType);
    return this.windAccountMapper.getAccountByAccountName(windAccount);
  }
  
  @Override
  public WindAccount getWindAccountDetailByAccountId(String accountId) {
    return this.windAccountMapper.selectById(accountId);
  }
  
  @Override
  public WindUser getBoundWindUser(String accountId) {
    return this.windAccountMapper.getBoundUser(accountId);
  }
  
  @Override
  public void addWindAccount(WindAccount windAccount) {
    this.windAccountMapper.insert(windAccount);
  }
  
  @Override
  public void updateWindAccount(WindAccount windAccount) {
    this.windAccountMapper.updateById(windAccount);
  }
  
  /**
   * 删除一个账户(包含物理删除与逻辑删除)
   * @param accountId 要删除的账户id
   */
  @Override
  public void deleteWindAccount(String accountId) throws AccountNotExistException {
    var findWindAccount = this.windAccountMapper.selectById(accountId);
    if (null == findWindAccount) {
      throw new AccountNotExistException("您要删除的账户不存在，请重试!");
    }
    var bindWindUser = this.windAccountMapper.getBoundUser(accountId);
    if (null == bindWindUser || null == bindWindUser.getUserStatus() || DataStatus.Normal !=  bindWindUser.getUserStatus()) {
      this.windAccountMapper.deleteById(accountId);
    } else {
      // 用户状态是正常的话，需要进行逻辑删除
      var updateWindUser = new WindUser();
      updateWindUser.setUserStatus(DataStatus.Deleted);
      updateWindUser.setUserStatusText(DataStatus.Deleted.getText());
      this.windUserMapper.changeWindUserStatus(updateWindUser);
    }
  }
  
  /**
   * 给账户绑定对应的用户
   *
   * @param accountId 账户id
   * @param userId  要绑定的用户id
   */
  @Override
  public void setBindUser(String accountId, String userId) throws AccountNotExistException, UserNotExistException {
    var windAccount = this.windAccountMapper.selectById(accountId);
    if (null == windAccount) {
      throw new AccountNotExistException("该账号不存在，请重试!");
    }
    var windUser = this.windUserMapper.selectById(userId);
    if (null == windUser) {
      throw new UserNotExistException("您要绑定的用户不存在，请重试!");
    }
    this.windAccountMapper.setBindUser(accountId, userId);
  }
  
  @Autowired
  public void setWindUserMapper(WindUserMapper windUserMapper) {
    this.windUserMapper = windUserMapper;
  }
  
  @Autowired
  public void setWindAccountMapper(WindAccountMapper windAccountMapper) {
    this.windAccountMapper = windAccountMapper;
  }
  
  @Autowired
  public void setWindUserService(WindUserService windUserService) {
    this.windUserService = windUserService;
  }
}
